package com.cmcc.linlaker.ui.query.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cmcc.linlaker.R;
import com.cmcc.linlaker.ui.query.ImageScanActivity;
import com.cmcc.linlaker.ui.query.bean.QueryProjectBean;
import com.cmcc.linlaker.utils.DensityUtil;
import com.cmcc.linlaker.view.BigImageDialog;
import com.cmcc.linlaker.view.GridSpacingItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class QueryHouseApproveDetailAdapter extends RecyclerView.Adapter<QueryHouseApproveDetailAdapter.ViewHolder> {

    private List<QueryProjectBean.ProjectApprovalData> itemList;
    private Context mContext;

    public QueryHouseApproveDetailAdapter(Context mContext, List<QueryProjectBean.ProjectApprovalData> itemList) {
        this.itemList = itemList;
        this.mContext = mContext;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.query_farmer_approve_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        QueryProjectBean.ProjectApprovalData item = itemList.get(position);

        holder.text_approve_code.setText(item.docNum);

        holder.text_approve_title.setText(item.matter);
        if (!TextUtils.isEmpty(item.status)) {
            if (item.status.equals("1")) {
                holder.text_approve_result.setText("已通过");
            } else if (item.status.equals("9")) {
                holder.text_approve_result.setText("待审批");
            } else {
                holder.text_approve_result.setText("未通过");
            }
        } else {
            holder.text_approve_result.setText("无");
        }

        holder.text_approve_date.setText(item.processTime);

        FileImageAdapter adapter = new FileImageAdapter(mContext, parseFiles(itemList.get(position).projectApprovalFiles));
        adapter.setOnItemClickListener(new FileImageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(mContext, ImageScanActivity.class);
                intent.putExtra("imageList", parseFiles(itemList.get(position).projectApprovalFiles));
                intent.putExtra("position", position);
                mContext.startActivity(intent);

//                BigImageDialog dialog = new BigImageDialog(mContext, R.style.dialog, parseFiles(itemList.get(position).projectApprovalFiles).get(position));
//                dialog.setCanceledOnTouchOutside(true);
//                dialog.show();
            }
        });
        holder.recycler_file.setAdapter(adapter);
    }

    private ArrayList<String> parseFiles(List<QueryProjectBean.ProjectFileData> projectFiles) {
        ArrayList<String> urlList = new ArrayList<>();

        for (int i = 0; i < projectFiles.size(); i++) {
            QueryProjectBean.ProjectFileData data = projectFiles.get(i);
            String url = data.fastDfsServer + data.groupName + "/" + data.fileName;
            urlList.add(url);
        }

        return urlList;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public TextView text_approve_title, text_approve_code;
        public TextView text_approve_result, text_approve_date;
        public RecyclerView recycler_file;

        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);

            text_approve_title = itemView.findViewById(R.id.text_approve_title);
            text_approve_code = itemView.findViewById(R.id.text_approve_code);
            text_approve_result = itemView.findViewById(R.id.text_approve_result);
            text_approve_date = itemView.findViewById(R.id.text_approve_date);

            recycler_file = itemView.findViewById(R.id.recycler_file);
            recycler_file.setHasFixedSize(true);
            recycler_file.setLayoutManager(new GridLayoutManager(mContext, 4));

            recycler_file.setNestedScrollingEnabled(false);
            recycler_file.addItemDecoration(new GridSpacingItemDecoration(4, DensityUtil.dip2px(mContext, 8), true));

        }

        @Override
        public void onClick(View v) {
//            onItemClickListener.onItemClick(getAdapterPosition());
        }
    }

}

